package com.spring.mvc.controller;

import static com.spring.mvc.util.UrlManager.URL_CHECKISLOGIN;
import static com.spring.mvc.util.UrlManager.URL_CHECKNAME;
import static com.spring.mvc.util.UrlManager.URL_LOGIN;
import static com.spring.mvc.util.UrlManager.URL_REGISTER;
import static com.spring.mvc.util.UrlManager.USER_PATH_MAPP;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.mvc.http.util.HttpUtil;
import com.spring.mvc.pojo.CallResult;
import com.spring.mvc.pojo.User;
import com.spring.mvc.service.UserService;
import com.spring.mvc.util.ErrCode;
import com.spring.mvc.util.Prompt;

@Controller
@RequestMapping(USER_PATH_MAPP)
public class UserController {
	
	private static final Logger logger=LoggerFactory.getLogger(UserController.class);
	private static final String USERID="userId";
	
	@Autowired
	private UserService userService;
	
	/**
	 * user login controller
	 * @param userName
	 * @param userPwd
	 * @param request
	 * @return
	 */
	@RequestMapping(value=URL_LOGIN,method=RequestMethod.POST)
	public @ResponseBody CallResult login(
			User user,HttpServletRequest request){
		CallResult result=new CallResult();
		if(user!=null && request!=null){
			result=userService.login(user);
			if(result.getErrCode()==ErrCode.SUCCESS){
				request.getSession().setAttribute(USERID, result.getObject());
			}
		}else{
			result.setErrInfo(ErrCode.ERROR, Prompt.get("error"));
			logger.error("System error!");
		}
		return result;
	}
	
	@RequestMapping(value=URL_CHECKISLOGIN)
	public @ResponseBody CallResult checkIsLogin(HttpServletRequest request){
		CallResult result=new CallResult();
		if(request!=null){
			User user=(User)request.getSession().getAttribute(USERID);
			if(user!=null){
				result.setErrInfo(ErrCode.SUCCESS, Prompt.get("success"));
			}else{
				result.setErrInfo(ErrCode.ERROR, Prompt.get("error"));
			}
		}else{
			result.setErrInfo(ErrCode.ERROR, Prompt.get("error"));
			logger.error("System error!");
		}
		return result;
	}
	
	/**
	 * check User name isExists
	 * @param userName
	 * @return
	 */
	@RequestMapping(URL_CHECKNAME)
	public @ResponseBody CallResult check(
			@RequestParam("userName")String userName){
		CallResult result=userService.check(userName);
		return result;
	}
	
	/**
	 * user register method
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value=URL_REGISTER)
	public @ResponseBody CallResult register(
			HttpServletRequest request,User user){
		CallResult result=null;
		if(request!=null && user!=null){
			if(!HttpUtil.isCheckCode(request, user.getCheckCode())){
				result=new CallResult();
				result.setErrInfo(ErrCode.ERROR, 
						Prompt.get("user.checkcode.error"));
			}else{
				String ip=HttpUtil.getIpAddr(request);
				user.setIpAddr(ip);
				result=userService.addUser(user);
			}
		}else{
			logger.info("System error!");
		}
		return result;
	}
	
	@RequestMapping("/download")
	public String downLoad(String code){
		System.out.println(code);
		return "success";
	}
	
	/**
	 * query user List
	 * @return
	 */
	/*@RequestMapping("/getList")
	public @ResponseBody CallResult getList(){
		CallResult result=userService.list();
		return result;
	}*/
	
/*	@RequestMapping("/hello/{name}")
	public String getInfor(@PathVariable("name")String name,@RequestParam("age")String age,User hw){
		System.out.println(4);
		return "success";
	}*/
	
	/*@RequestMapping("/test")
	public @ResponseBody String test(
				HttpServletRequest request,HttpServletResponse response){
		System.out.println(request.getRemoteAddr());
		System.out.println(response);
		return "hello";
	}*/
	
	
	/*@RequestMapping(value="/pass")
	public String pass(@RequestParam String user_pwd){
		System.out.println(user_pwd);
		return "success";
	}*/
}
